#include "OperatorStack.h"
#include <QMap>

template class CalcStack<QChar>;

//初始化优先级表
OperatorStack::OperatorStack(): m_priority
                                (new QMap<QChar, int>({{u'×', 1}, {u'÷', 1}, {u'+', 2}, {u'-', 2}}))
{

}

OperatorStack::~OperatorStack()
{
    delete m_priority;
}


bool OperatorStack::push(const QChar & op)
{
    if (isFull() || !isValid(op))
        return false;
    //若栈为空，或当前待入栈操作符的优先级高于当前栈顶操作符的优先级，则入栈
    if (!isEmpty() && m_priority->constFind(op).value() < m_priority->constFind(top()).value())
    {
        CalcStack::push(op);
        return true;
    }
    //栈不为空，且当前待入栈操作符的优先级低于当前栈顶，则不入栈
    else
        return false;
}

bool OperatorStack::isValid(const QChar &op) const
{
    return m_priority->constFind(op) != m_priority->constEnd();
}
